﻿using BLL.Interface;
using Common;
using Model;
using Model.Entity.Article;
using Model.Entity.Type;
using Model.Entity.User;
using Model.Entity.User.Relation;
using Model.Enum;

namespace BLL
{
    /// <summary>
    /// 系统业务逻辑层
    /// </summary>
    public class SystemBLL : ISystemBLL
    {
        private readonly TianXiaHuiForumManagementSystemDbContext _dbContext;
        public SystemBLL(TianXiaHuiForumManagementSystemDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 初始化数据库
        /// </summary>
        /// <exception cref="Exception"></exception>
        public void InitDatabase()
        {
            //不能放到事务中，因为会导致无法找到数据库
            _dbContext.Database.EnsureDeleted();
            _dbContext.Database.EnsureCreated();
            using var tran = _dbContext.Database.BeginTransaction();
            try
            {
                int result;
                #region 初始化文章类型
                TB_ArticleType[] articleTypes = new[]
                {
                    new TB_ArticleType
                    {
                        Name = "文学",
                        Code = ArticleType.Literature.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_ArticleType
                    {
                        Name = "游戏",
                        Code = ArticleType.Game.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_ArticleType
                    {
                        Name = "生活",
                        Code = ArticleType.Life.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in articleTypes)
                {
                    _dbContext.TB_ArticleType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != articleTypes.Length)
                {
                    throw new Exception("初始化文章类型失败");
                }
                #endregion
                #region 初始化审核类型
                TB_CheckType[] checkTypes = new[]
                {
                    new TB_CheckType
                    {
                        Name = "未审核",
                        Code = CheckType.Unchecked.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_CheckType
                    {
                        Name = "已通过",
                        Code = CheckType.Success.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_CheckType
                    {
                        Name = "未通过",
                        Code = CheckType.Failed.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in checkTypes)
                {
                    _dbContext.TB_CheckType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != checkTypes.Length)
                {
                    throw new Exception("初始化审核类型失败");
                }
                #endregion;
                #region 初始化性别类型
                TB_GenderType[] genderTypes = new[]
                {
                    new TB_GenderType
                    {
                        Name = "未知",
                        Code = GenderType.Unknown.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_GenderType
                    {
                        Name = "男性",
                        Code = GenderType.Male.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_GenderType
                    {
                        Name = "女性",
                        Code = GenderType.Female.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in genderTypes)
                {
                    _dbContext.TB_GenderType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != genderTypes.Length)
                {
                    throw new Exception("初始化性别类型失败");
                }
                #endregion
                #region 初始化会员类型
                TB_MemberType[] memberTypes = new[]
                {
                    new TB_MemberType
                    {
                        Name = "普通会员",
                        Code = MemberType.Normal.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_MemberType
                    {
                        Name = "超级会员",
                        Code = MemberType.Super.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in memberTypes)
                {
                    _dbContext.TB_MemberType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != memberTypes.Length)
                {
                    throw new Exception("初始化会员类型失败");
                }
                #endregion
                #region 初始化支付状态类型
                TB_PaymentStateType[] paymentStateTypes = new[]
                {
                    new TB_PaymentStateType
                    {
                        Name = "未支付",
                        Code = PaymentStateType.Unpaid.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_PaymentStateType
                    {
                        Name = "已支付",
                        Code = PaymentStateType.Paid.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in paymentStateTypes)
                {
                    _dbContext.TB_PaymentStateType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != paymentStateTypes.Length)
                {
                    throw new Exception("初始化支付状态类型失败");
                }
                #endregion
                #region 初始化支付类型
                TB_PaymentType[] paymentTypes = new[]
                {
                    new TB_PaymentType
                    {
                        Name = "支付宝支付",
                        Code = PaymentType.Alipay.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_PaymentType
                    {
                        Name = "微信支付",
                        Code = PaymentType.Wechat.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in paymentTypes)
                {
                    _dbContext.TB_PaymentType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != paymentTypes.Length)
                {
                    throw new Exception("初始化支付类型失败");
                }
                #endregion
                #region 初始化状态类型
                TB_StateType[] stateTypes = new[]
                {
                    new TB_StateType
                    {
                        Name = "正常",
                        Code = StateType.Normal.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_StateType
                    {
                        Name = "封禁",
                        Code = StateType.Ban.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_StateType
                    {
                        Name = "注销",
                        Code = StateType.Cancel.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in stateTypes)
                {
                    _dbContext.TB_StateType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != stateTypes.Length)
                {
                    throw new Exception("初始化状态类型失败");
                }
                #endregion
                #region 初始化用户类型
                TB_UserType[] userTypes = new[]
                {
                    new TB_UserType
                    {
                        Name = "管理员",
                        Code = UserType.Admin.ToString(),
                        Description = "所有权限所有者",
                        Remark = "系统生成"
                    },
                    new TB_UserType
                    {
                        Name = "编辑者",
                        Code = UserType.Editor.ToString(),
                        Description = "部分权限所有者",
                        Remark = "系统生成"
                    },
                    new TB_UserType
                    {
                        Name = "用户",
                        Code = UserType.User.ToString(),
                        Description = "用户权限所有者",
                        Remark = "系统生成"
                    }
                };
                foreach (var item in userTypes)
                {
                    _dbContext.TB_UserType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != userTypes.Length)
                {
                    throw new Exception("初始化角色类型失败");
                }
                #endregion
                #region 初始化商品类型
                TB_WareType[] wareTypes = new[]
                {
                    new TB_WareType
                    {
                        Name = "食品",
                        Code = WareType.Food.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_WareType
                    {
                        Name = "服装",
                        Code = WareType.Clothing.ToString(),
                        Remark = "系统生成"
                    },
                    new TB_WareType
                    {
                        Name = "电子产品",
                        Code = WareType.Electroic.ToString(),
                        Remark = "系统生成"
                    }
                };
                foreach (var item in wareTypes)
                {
                    _dbContext.TB_WareType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != wareTypes.Length)
                {
                    throw new Exception("初始化商品类型失败");
                }
                #endregion

                #region 初始化用户
                string passwordSalt = SecurityHelper.GenerateSaltHex();
                string password = SecurityHelper.MD5Encrypt("123456");
                TB_User[] users = new[]
                {
                    new TB_User
                    {
                        UserName = "admin",
                        PhoneNumber = "SystemPhone",
                        Email = "SystemEmail",
                        PasswordSalt = passwordSalt,
                        Password = SecurityHelper.SHA256Encrypt(passwordSalt + password),
                        AvatarPath = "DefaultAvatar.jpg",
                        UserTypeId = (int)UserType.Admin,
                        StateTypeId = (int)StateType.Normal,
                        Remark = "系统生成"
                    }
                };
                foreach (var item in users)
                {
                    _dbContext.TB_User.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != users.Length)
                {
                    throw new Exception("初始化用户失败");
                }
                #endregion
                #region 初始化用户角色
                TB_UserToType[] userToTypes = new[]
                {
                    new TB_UserToType
                    {
                        UserId = users.First().Id,
                        UserTypeId = users.First().UserTypeId,
                        Remark = "系统生成"
                    }
                };
                foreach (var item in userToTypes)
                {
                    _dbContext.TB_UserToType.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != userToTypes.Length)
                {
                    throw new Exception("初始化用户角色失败");
                }
                #endregion
                #region 初始化文章
                TB_Article[] articles = new[]
                {
                    new TB_Article
                    {
                        Title = "欢迎来到天下会论坛",
                        Content = "金鳞岂是池中物，一遇风云变化龙",
                        UserId = users.First().Id,
                        CheckTypeId = (int)CheckType.Success,
                        StateTypeId= (int)StateType.Normal,
                        ViewNumber = 0,
                        LikeNumber = 0,
                        CommentNumber = 0,
                        CollectNumber = 0,
                        Remark = "系统生成"
                    },
                    new TB_Article
                    {
                        Title = "秋去秋来",
                        Content = "秋去秋来冷落花，寒风利刃断千佳。冰霜骨冻心莫怪，余温只应送梅花。",
                        UserId = users.First().Id,
                        CheckTypeId = (int)CheckType.Success,
                        StateTypeId= (int)StateType.Normal,
                        ViewNumber = 0,
                        LikeNumber = 0,
                        CommentNumber = 0,
                        CollectNumber = 0,
                        Remark = "系统生成"
                    }
                };
                foreach (var item in articles)
                {
                    _dbContext.TB_Article.Add(item);
                }
                result = _dbContext.SaveChanges();
                if (result != articles.Length)
                {
                    throw new Exception("初始化文章失败");
                }
                #endregion
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw new Exception("初始化数据库失败：" + ex.Message);
            }
        }
    }
}
